using System.Collections.Generic;
using System.IO;
using Codice.Client.BaseCommands;
using Unity.Plastic.Newtonsoft.Json;
using Unity.Plastic.Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;

public class DevlopPackageDependCreator : EditorWindow
{
    
    private static ListRequest listRequest;
    
    private static Dictionary<string, bool> curStatePackageMark = new Dictionary<string, bool>();
    private static Dictionary<string, string> curStatePackageUrl = new Dictionary<string, string>();
    
    [MenuItem("工程构建/构建工程包结构依赖")]
    private static void CreateProjectPackage()
    {
        curStatePackageMark.Clear();
        curStatePackageUrl.Clear();
        
        //upm标记
        curStatePackageMark.Add("com.cysharp.unitask",false);
        
        //实际加载地址
        curStatePackageUrl.Add("com.cysharp.unitask","https://gitee.com/dream408/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask");
        
        //获取当前包里面的插件目录
        listRequest = Client.List();
        EditorApplication.update += UpdateProgress;
        
    }
    
    
    static void UpdateProgress()
    {
        if (listRequest.IsCompleted)
        {

            if (listRequest.Status == StatusCode.Success)
            {
                //循环遍历
                foreach (var packageNode in listRequest.Result)
                {
                    if (curStatePackageMark.ContainsKey(packageNode.name))
                    {
                        curStatePackageMark[packageNode.name] = true;
                    }
                }

                Dictionary<string, string> addPack = new Dictionary<string, string>();
                
                //标记结束后添加未使用的package
                foreach (var node in curStatePackageMark)
                {
                    if (!node.Value)
                    {
                        //添加
                        string name = node.Key;
                        string url = curStatePackageUrl[name];

                        addPack.Add(name,url);
                    }
                }
                
                IO_PackageWrite(addPack);
                
            }
            
            
            EditorApplication.update -= UpdateProgress;
            
            //手动刷新package
            Client.Resolve();
        }
    }
    
    private static void IO_PackageWrite(Dictionary<string,string> addPack)
    {
        //查找到文件
        string path = Application.dataPath + "/../Packages/manifest.json";
        string content = File.ReadAllText(path);
        //解析json  为了不和本体的LitJson产生引用关系 决定使用Newtonsoft.Json
        JObject packageData = JsonConvert.DeserializeObject(content) as JObject;
        //dependencies 对象
        var dependencies = packageData["dependencies"];
        //将数据添加到dependencies 
        foreach (var node in addPack)
        {
            dependencies[node.Key] = node.Value;
        }
        
        //添加完毕后生成一个新文件
        string newContent = JsonConvert.SerializeObject(packageData,Formatting.Indented);
        //写入文件
        File.WriteAllText(path,newContent);
        
    }
}
